<?php  
define("TOKEN", "weixin");  
$wechatObj = new wechatCallbackapiTest();  
$wechatObj->responseMsg();  
  
class wechatCallbackapiTest  
{  
    public function responseMsg()  
    {  
        $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];  
        if (!empty($postStr)){  
            $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);  
            $RX_TYPE = trim($postObj->MsgType);  
  
            switch ($RX_TYPE)  
            {  
                case "text":  
                    $resultStr = $this->receiveText($postObj);  
                    break;  
                case "event":  
                    $resultStr = $this->receiveEvent($postObj);  
                    break;  
                default:  
                    $resultStr = "unknow msg type: ".$RX_TYPE;  
                    break;  
            }  
            echo $resultStr;  
        }else {  
            echo "";  
            exit;  
        }  
    }  
  
    private function receiveText($object)  
    {  
        $funcFlag = 0;  
        $keyword = trim($object->Content);  
        $resultStr = "";  
        $cityArray = array();  
        $contentStr = "";  
        $needArray = false;  
        $illegal = false;  
        $saytome = false;  
  
        if ($keyword == "Hello2BizUser"){  
            $contentStr = "欢迎关注白云机场交流论坛，这其实是老的欢迎词，你关注时收不到了";  
            $resultStr = $this->transmitText($object, $contentStr, $funcFlag);  
            return $resultStr;  
        }else {  
  
        }  
    }  
  
    private function receiveEvent($object)  
    {  
        $contentStr = "";  
        switch ($object->Event)  
        {  
            case "subscribe":  
                $contentStr = "您好，欢迎关注白云机场交流论坛！";  
                break;  
        }  
        $resultStr = $this->transmitText($object, $contentStr);  
        return $resultStr;  
    }  
  
    private function transmitText($object, $content, $flag = 0)  
    {  
        $textTpl = "<xml>  
<ToUserName><![CDATA[%s]]></ToUserName>  
<FromUserName><![CDATA[%s]]></FromUserName>  
<CreateTime>%s</CreateTime>  
<MsgType><![CDATA[text]]></MsgType>  
<Content><![CDATA[%s]]></Content>  
<FuncFlag>%d</FuncFlag>  
</xml>";  
        $resultStr = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content, $flag);  
        return $resultStr;  
    }  
}  
  
?> 